package com.ruoyi.community.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.community.domain.vo.HousingVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.community.domain.Housing;
import com.ruoyi.community.service.IHousingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 小区Controller
 *
 * @author lj
 * @date 2022-02-22
 */
@RestController
@RequestMapping("/community/housing")
public class HousingController extends BaseController
{
    @Autowired
    private IHousingService housingService;

    /**
     * 查询小区列表
     */
    @PreAuthorize("@ss.hasPermi('community:housing:list')")
    @GetMapping("/list")
    public TableDataInfo list(Housing housing)
    {
        startPage();
        List<HousingVo> list = housingService.selectHousingVoList(housing);
        for (HousingVo h : list) {
            h.setPca(h.getProvinceName()+"-"+h.getCityName()+"-"+h.getAreaName());
        }
        return getDataTable(list);
    }
    @GetMapping("/lists")
    public AjaxResult lists(Housing housing)
    {
        List<Housing> list = housingService.selectHousingList(housing);
        return AjaxResult.success(list);
    }

    /**
     * 导出小区列表
     */
    @PreAuthorize("@ss.hasPermi('community:housing:export')")
    @Log(title = "小区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Housing housing)
    {
        List<Housing> list = housingService.selectHousingList(housing);
        ExcelUtil<Housing> util = new ExcelUtil<Housing>(Housing.class);
        util.exportExcel(response, list, "小区数据");
    }

    /**
     * 获取小区详细信息
     */
    @PreAuthorize("@ss.hasPermi('community:housing:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(housingService.selectHousingById(id));
    }

    /**
     * 新增小区
     */
    @PreAuthorize("@ss.hasPermi('community:housing:add')")
    @Log(title = "小区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Housing housing)
    {
        return toAjax(housingService.insertHousing(housing));
    }

    /**
     * 修改小区
     */
    @PreAuthorize("@ss.hasPermi('community:housing:edit')")
    @Log(title = "小区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Housing housing)
    {
        if(housing.getId()==1){
            return AjaxResult.error("禁止修改此小区");
        }
        return toAjax(housingService.updateHousing(housing));
    }

    /**
     * 删除小区
     */
    @PreAuthorize("@ss.hasPermi('community:housing:remove')")
    @Log(title = "小区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (int i = 0; i < ids.length; i++) {
            if(ids[i]==1){
                return AjaxResult.error("禁止删除此小区");
            }
        }
        return toAjax(housingService.updateHousingByIds(ids));
    }
}
